﻿/*
 * GameBoard.cs
 *
 * Created on November 22, 2007, 6:50 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * Copyright (C) 2010       Mathijs Miermans
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using HexTD.util;

namespace HexTD.UI
{
	public class GameBoard : Canvas
	{
		private HexTD game;
		private Context context;
		private float offsetX, offsetY;

		/**
		 * Creates new form GameBoard
		 */
		public GameBoard(HexTD game, Context context)
		{
			this.game = game;
			this.context = context;
#if PORT
        initComponents();
#endif
		}

		public void paint(Canvas g)
		{
//        try {
            this.game.paintBoard(g);
//        } catch (IllegalArgumentException e) {
//            this.game.recalculateScale();
//            this.game.repaint();
//        } catch (NullPointerException e) {
//            this.game.recalculateScale();
//            this.game.repaint();
//        }
		}

		public int[] hexCoordsFromScreenCoords(int screenx, int screeny)
		{
			int row, column, hexX, hexY;
			float x, y, dx, dy, h, r, width, height;
			int[] hexC = new int[2];
			width = this.context.hexPaintWidth * 2;
			height = this.context.paintScale * 1.5f;
			h = this.context.paintScale / 2;
			r = this.context.hexPaintWidth;
			x = screenx - this.context.offsetX;
			y = screeny - this.context.offsetY;
			column = (int)Math.Round(-0.5f + x / width);
			row = (int)Math.Round(-0.5f + y / height);
			dx = x - column * width;
			dy = y - row * height;

			hexX = column;
			hexY = row;
			if ((row & 1) == 0)
			{
				if (dy < (h - (h / r) * dx))
				{
					hexX--;
					hexY--;
				}
				if (dy < (-h + (h / r) * dx))
				{
					hexY--;
				}
			}
			else
			{
				if (dx > r)
				{
					if (dy < h * 2 - h / r * dx)
					{
						hexY--;
					}
				}
				else
				{
					if (dy < h / r * dx)
					{
						hexY--;
					}
					else
					{
						hexX--;
					}
				}
			}
			hexC[0] = hexX;
			hexC[1] = hexY;
			return hexC;
		}

		/** This method is called from within the constructor to
		 * initialize the form.
		 * WARNING: Do NOT modify this code. The content of this method is
		 * always regenerated by the Form Editor.
		 */
		// <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
		private void initComponents()
		{
#if PORT
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                formKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
#endif
		}// </editor-fold>                        

#if PORT
    private void formKeyTyped(java.awt.event.KeyEvent evt) {                              
        this.game.keyTyped(evt.getKeyChar());
    }   
#endif
	}
}

